package at.fhj.itm11.forum.service.converter;

import java.util.ArrayList;
import java.util.List;

import at.fhj.itm11.forum.model.Comment;
import at.fhj.itm11.forum.model.Post;
import at.fhj.itm11.forum.service.dtos.CommentDTO;
import at.fhj.itm11.forum.service.dtos.PostDTO;

public class CommentDTOConverter implements DTOConverter<CommentDTO, Comment> {
	private DTOConverter<PostDTO, Post> postDtoConverter = new PostDTOConverter();

	@Override
	public CommentDTO convertToDto(Comment commentEntity) {
		CommentDTO dto = new CommentDTO();
		
		dto.setDate(commentEntity.getDate());
		dto.setText(commentEntity.getText());
		dto.setUser(commentEntity.getUser());
		dto.setPost(postDtoConverter.convertToDto(commentEntity.getPost()));
		
		return dto;
	}

	@Override
	public List<CommentDTO> convertToDto(List<Comment> commentEntities) {
		List<CommentDTO> commentDtoList = new ArrayList<CommentDTO>();
		
		for(Comment c : commentEntities) {
			commentDtoList.add(convertToDto(c));
		}
		
		return commentDtoList;
	}

	@Override
	public Comment convertToEntity(CommentDTO commentDto) {
		Comment c = new Comment();
    	
    	c.setDate(commentDto.getDate());
    	c.setText(commentDto.getText());
    	c.setUser(commentDto.getUser());
    	c.setPost(postDtoConverter.convertToEntity(commentDto.getPost()));
    	
    	return c;
	}
}
